package com.flyco.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.entity.OmsOrderSetting;
import com.flyco.modules.oms.entity.OmsOrderTrace;
import com.flyco.modules.oms.mapper.OmsOrderSettingMapper;
import com.flyco.modules.oms.mapper.OmsOrderTraceMapper;
import com.flyco.modules.oms.service.IOmsOrderService;
import com.flyco.modules.oms.service.IOmsOrderSettingService;
import com.flyco.modules.stm.entity.StmBizInoutRelation;
import com.flyco.modules.stm.mapper.StmBizInoutRelationMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.manager.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 订单设置
 * @Author: flyco
 * @Date: 2019-12-20
 * @Version: V1.0
 */
@Service
@Slf4j
public class OmsOrderSettingServiceImpl extends ServiceImpl<OmsOrderSettingMapper, OmsOrderSetting> implements IOmsOrderSettingService {

    @Autowired
    private IOmsOrderService omsOrderService;
    @Autowired
    private OmsOrderTraceMapper omsOrderTraceMapper;
    @Autowired
    private StmBizInoutRelationMapper stmBizInoutRelationMapper;

    @Override
    public boolean orderReceiveConfirm() {

        //发货后自动确认收货时间
        LambdaQueryWrapper<OmsOrderSetting> query = new LambdaQueryWrapper<>();
        OmsOrderSetting omsOrderSetting = getOne(query);

        Integer confirmOvertime = omsOrderSetting.getConfirmOvertime();

        //获取已经发货但未收货的订单(注：发货单已全部发货且超过设置的过期时间)
        LambdaQueryWrapper<OmsOrder> orderQuery = new LambdaQueryWrapper<>();
        orderQuery.eq(OmsOrder::getStatus, 2)
                .eq(OmsOrder::getSendType, 1)
                .le(OmsOrder::getDeliveryTime, LocalDate.now().minusDays(confirmOvertime));
        List<OmsOrder> orderList = omsOrderService.list(orderQuery);

        if (CollectionUtils.isEmpty(orderList)) {
            return true;
        }

        //把满足条件的订单设置成已收货
        orderList.stream().map(o -> {
            o.setStatus(3);
            o.setReceiveTime(new Date());
            o.setConfirmStatus(1);
            return o;
        }).collect(Collectors.toList());


        for (Integer i = 0; i < orderList.size(); i++) {
            OmsOrder order = orderList.get(i);
            omsOrderService.updateById(order);

            //订单轨迹
            OmsOrderTrace trace = new OmsOrderTrace();
            trace.setOrderId(order.getId());
            trace.setLabel("确认收货");
            trace.setContent("");
            trace.setCreateTime(new Date());
            omsOrderTraceMapper.insert(trace);
            //确认收货不再写
//            createStockIn(order);
        }

        log.info("订单自动修改成收货状态的数量是：" + orderList.size());

        return true;
    }

    /**
     * 收货信息初始化
     */
    private void createStockIn(OmsOrder omsOrder) {

        Date date = new Date();
        StmBizInoutRelation stmBizInoutRelation = new StmBizInoutRelation();
        stmBizInoutRelation.setPartnerId(omsOrder.getPartnerId());
        //门店信息
        stmBizInoutRelation.setStoreId(omsOrder.getStoreId());
        stmBizInoutRelation.setStoreName(omsOrder.getReceiverName());
        //发货门店信息
        stmBizInoutRelation.setSourceStoreId(0L);
        stmBizInoutRelation.setSourceStoreName("总仓");
        stmBizInoutRelation.setStatus(1);
        stmBizInoutRelation.setType("STOCK_IN");
        stmBizInoutRelation.setStockBizType("STOCK_IN_TYPE_PURCH");
        stmBizInoutRelation.setSourceType("RECEIPT_TYPE_PURCHASE");
        stmBizInoutRelation.setSourceId(Long.parseLong(omsOrder.getId()));
        stmBizInoutRelation.setOrderId(omsOrder.getId());
        stmBizInoutRelation.setCreateBy("system");
        stmBizInoutRelation.setCreateTime(date);
        stmBizInoutRelationMapper.insert(stmBizInoutRelation);
    }


    @Override
    public OmsOrderSetting getOne() {
        List<OmsOrderSetting> omsOrderSettings = list();
        if (CollectionUtils.isEmpty(omsOrderSettings)) {
            return null;
        }
        return omsOrderSettings.get(0);
    }
}
